package com.yiwyn.ood.lod;

import java.util.ArrayList;
import java.util.List;



public class Main {
    public static void main(String[] args) {
        // 书店
        BookStore store = new BookStore();


        // 购物车
        ShoppingCart cart = new ShoppingCart();

        // 补充说明：如果每个客户不通过购物车这个实体，直接和书店交互，那么系统设计上就会出现大问题，书店中要存在大量的无关信息。这种情况下就违反了迪米特法则
        // 客户和书店之前的联系过于密切


        // 显示书店中的书
        store.displayAvailableBooks();

        // 用户将书添加到购物车
        store.addBookToCart(cart, "1984");
        store.addBookToCart(cart, "The Great Gatsby");

        // 显示购物车内容
        cart.displayCart();
    }
}

// 书类
class Book {
    private String title;
    private double price;

    Book(String title, double price) {
        this.title = title;
        this.price = price;
    }

    String getTitle() {
        return title;
    }

    double getPrice() {
        return price;
    }
}

// 购物车类
class ShoppingCart {
    private List<Book> books;

    ShoppingCart() {
        this.books = new ArrayList<>();
    }

    void addBook(Book book) {
        books.add(book);
    }

    double calculateTotalPrice() {
        double total = 0;
        for (Book book : books) {
            total += book.getPrice();
        }
        return total;
    }

    void displayCart() {
        System.out.println("Books in the shopping cart:");
        for (Book book : books) {
            System.out.println("- " + book.getTitle() + " ($" + book.getPrice() + ")");
        }
        System.out.println("Total price: $" + calculateTotalPrice());
    }
}

// 书店类
class BookStore {
    private List<Book> availableBooks;

    BookStore() {
        // 初始化一些书
        this.availableBooks = new ArrayList<>();
        availableBooks.add(new Book("The Great Gatsby", 10.99));
        availableBooks.add(new Book("1984", 8.99));
        availableBooks.add(new Book("To Kill a Mockingbird", 12.99));
    }

    void displayAvailableBooks() {
        System.out.println("Available books in the store:");
        for (Book book : availableBooks) {
            System.out.println("- " + book.getTitle() + " ($" + book.getPrice() + ")");
        }
    }

    void addBookToCart(ShoppingCart cart, String title) {
        for (Book book : availableBooks) {
            if (book.getTitle().equals(title)) {
                cart.addBook(book);
                System.out.println("Added to cart: " + book.getTitle());
                return;
            }
        }
        System.out.println("Book not found: " + title);
    }
}


